package com.neiquan.backstage.platform_system_config.web.action;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.platform_system_config.PlatformSystemConfigService;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformSystemConfig;

@Controller
@RequestMapping(value = "platformSystemConfig", produces = { "text/json;charset=UTF-8" })
public class PlatformSystemConfigAction {
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private PlatformSystemConfigService systemConfigService;

	@ResponseBody
	@RequestMapping(value = "query")
	public String query() {
		if (systemConfigService.query() == null) {
			systemConfigService.init();
		}
		return CommonResp.buildSuccessResp(systemConfigService.query()).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "edit")
	public String edit(String weixinAppId, String weixinAppSecret, String weixinToken, String weixinEncodingAesKey, String weixinH5UrlArticle, String weixinH5UrlAllianceMember, String weixinH5UrlEntrepreneurService) {
		PlatformSystemConfig toUpdate = systemConfigService.query();
		if (null == toUpdate) {
			systemConfigService.init();
			toUpdate = systemConfigService.query();
		}
		if (StringUtils.isNoneBlank(weixinAppId)) {
			toUpdate.setWeixinAppId(weixinAppId.trim());
		}
		if (StringUtils.isNoneBlank(weixinAppSecret)) {
			toUpdate.setWeixinAppSecret(weixinAppSecret.trim());
		}
		if (StringUtils.isNoneBlank(weixinToken)) {
			toUpdate.setWeixinToken(weixinToken.trim());
		}
		if (StringUtils.isNoneBlank(weixinEncodingAesKey)) {
			toUpdate.setWeixinEncodingAesKey(weixinEncodingAesKey.trim());
		}
		if (StringUtils.isNoneBlank(weixinH5UrlArticle)) {
			toUpdate.setWeixinH5UrlArticle(weixinH5UrlArticle.trim());
		}
		if (StringUtils.isNoneBlank(weixinH5UrlAllianceMember)) {
			toUpdate.setWeixinH5UrlAllianceMember(weixinH5UrlAllianceMember.trim());
		}
		if (StringUtils.isNoneBlank(weixinH5UrlEntrepreneurService)) {
			toUpdate.setWeixinH5UrlEntrepreneurService(weixinH5UrlEntrepreneurService.trim());
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
